﻿using System.Data.OleDb;
namespace AnimeLibrary.App_Code
{
    public class WriteOp : ConnectionOp
    {
        //*************************************************
        //                   Methods
        //*************************************************
        #region OleDb Methods
        /// <summary>
        /// Add a new Anime listing
        /// </summary>
        /// <param name="databasePath">The full path of the database file to write into.</param>
        /// <param name="title">Title of the Anime.</param>
        /// <param name="author">Author of the Anime.</param>
        /// <param name="plotSummary">Summary of the Anime.</param>
        /// <param name="thumbnailLocation">Thumbnail Location of the Anime.</param>
        /// <param name="imageLocation">Image Location of the Anime.</param>
        /// <returns>True if the new Anime listing is saved, otherwise False.</returns>
        public static bool AddAnime(string databasePath, string title, string author, int noOfEpisodes, string plotSummary, string thumbnailLocation, string imageLocation)
        {
            string commandText = string.Format("INSERT INTO tbl_anime_library ([Title], [Author], [TotalEpisodes], [PlotSummary], [ThumbnailLocation], [ImageLocation]) VALUES ('{0}', '{1}', {2}, '{3}', '{4}', '{5}');", title.Replace("'", string.Empty), author.Replace("'", string.Empty), noOfEpisodes, plotSummary.Replace("'", string.Empty), thumbnailLocation.Replace("'", string.Empty), imageLocation.Replace("'", string.Empty));

            return ExecuteCommand(databasePath, commandText);
        }


        /// <summary>
        /// Executes a non query command.
        /// </summary>
        /// <param name="databasePath">The full path of the database file to write into.</param>
        /// <param name="commandText">The SQL command text to execute.</param>
        /// <returns>True if execution was successful, otherwise False.</returns>
        public static bool ExecuteCommand(string databasePath, string commandText)
        {
            return ExecuteCommand(databasePath, new OleDbCommand(commandText));
        }

        /// <summary>
        /// Executes a non query command.
        /// </summary>
        /// <param name="databasePath">The full path of the database file to write into.</param>
        /// <param name="oledbCommand">The SQL command to execute.</param>
        /// <returns>True if execution was successful, otherwise False.</returns>
        public static bool ExecuteCommand(string databasePath, OleDbCommand oledbCommand)
        {
            bool successful = false;

            try
            {
                if (OpenOleDbConnection(databasePath))
                {
                    oledbCommand.CommandTimeout = 0;
                    oledbCommand.Connection = OleDbConnection;
                    oledbCommand.ExecuteNonQuery();

                    successful = true;
                }
            }
            finally
            {
                CloseOleDbConnection();
            }

            return successful;
        }
        #endregion
        //*************************************************
    }
}